<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false"
    :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <el-form-item label="个人收藏名称" prop="dfvName">
        <el-input v-model="dataForm.dfvName" placeholder="展会项目名称" style="width: 300px;"></el-input>
      </el-form-item>
      <el-form-item label="共享对象" prop="dfvIfPublic">
        <ren-radio-group v-model="dataForm.dfvIfPublic" dict-type="share" @change="shareList()"></ren-radio-group>
      </el-form-item>
      <div v-if="dataForm.dfvIfPublic == 2">
        <el-form-item label="项目经理">
          <el-checkbox-group style="width: 800px" v-model="dataForm.shereIdList">
            <el-checkbox v-for="leader in taskleader" :key="leader.userId" :label="leader.userId" :value="leader.userId">
              {{ leader.realName }}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="工作人员">
          <el-checkbox-group style="width: 800px" v-model="dataForm.shereIdList">
            <el-checkbox v-for="leader in tskWork" :key="leader.userId" :label="leader.userId" :value="leader.userId">
              {{ leader.realName }}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </div>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { useI18n } from "vue-i18n";
import { ElMessage } from "element-plus";
const { t } = useI18n();
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  id: "",
  dfvName: "",
  dfvIfPublic: 0,
  dfvSource: "",
  shereIdList: [] as string[],

});
//做校验
const rules = ref({
  dfvName: [{ required: true, message: t("validate.required"), trigger: "blur" }],
});

const init = (id?: number) => {
  visible.value = true;
  dataForm.id = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (id) {
    getInfo(id);
  }
};

// 获取收藏夹信息
const getInfo = (id: number) => {
  baseService.get("/crm/crmdatafavorite/" + id).then((res) => {
    Object.assign(dataForm, res.data);
    //调用获取工作人员和工作人员列表
    shareList()
  });
};


//项目经理
const taskleader = ref();

//工作人员
const tskWork = ref();
const isListVisible = ref(false);

/**
 * 获取工作人员和工作人员列表
 */
const shareList = () => {
  if (dataForm.dfvIfPublic == 2) {
    isListVisible.value = true;
    // 3：项目经理
    baseService.get("/sys/user/usersByType/3").then((res) => {
      if (res.code !== 0) {
        return ElMessage.error(res.msg);
      }
      taskleader.value = res.data;
    });
    // 4：工作人员
    baseService.get("/sys/user/usersByType/4").then((res) => {
      if (res.code !== 0) {
        return ElMessage.error(res.msg);
      }
      tskWork.value = res.data;
    });
  } else {
    taskleader.value = null;
    tskWork.value = null;
  }
}

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.id ? baseService.post : baseService.put)("/crm/crmdatafavorite", { dfvIfPublic: Number(dataForm.dfvIfPublic), dfvName: dataForm.dfvName, shereIdList: dataForm.shereIdList, dfvSource: dataForm.dfvSource }).then((res) => {
      ElMessage.success({
        message: t("prompt.success"),
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>
<style lang="scss" scoped>
.el-checkbox {
  width: 100px;
}
</style>